import React, {PureComponent} from "react";
import {connect} from "react-redux";
import FormEditor from '../../components/FormEditor';
import {updateCarrierModel} from "./actions";
import {getPrincipal} from "../../lib/identity";

class CarrierModal extends PureComponent {

    handleUpdateCarrierModel = client => {
        const {dispatch, model} = this.props;
        dispatch(updateCarrierModel({...model, ...client}));
    };

    render() {
        const {model, organizations} = this.props;
        const schema = [
            {
                title: '基础信息',
                fields: [
                    {
                        field: 'code',
                        title: '供应商代码',
                        type: 'text',
                        fieldOptions: {
                            rules: [{required: true, message: '请输入供应商代码'}]
                        },
                    },{
                        field: 'name',
                        title: '供应商简称',
                        type: 'text',
                        fieldOptions: {
                            rules: [{required: true, message: '请输入供应商简称'}]
                        },
                    }, {
                        field: 'fullName',
                        title: '公司全称',
                        type: 'text',
                        fieldOptions: {
                            rules: [{required: true, message: '请输入公司全称'}]
                        },
                    }, {
                        field: 'licence',
                        title: '营业执照',
                        type: 'text'
                    }, {
                        field: 'settlementDays',
                        title: '结算周期',
                        placeholder: "单位(天)",
                        type: 'number'
                    }
                ]
            }];
        if (getPrincipal().admin) {
            schema[0].fields.splice(0, 0, {
                field: 'organizationId',
                title: '所属机构',
                type: 'treeSelector',
                readonly: model.status === "CONFIRMED",
                controlProps: {
                    treeData: organizations,
                    labelField: "name",
                    valueField: "id",
                    treeCheckable: false
                },
                fieldOptions: {
                    rules: [{required: true, message: '请选择所属机构'}]
                }
            });
        }
        return (

            <FormEditor
                schema={schema}
                column={3}
                defaultReadonly={false}
                showActionBar={false}
                defaultValues={model}
                onChange={this.handleUpdateCarrierModel}
                wrappedComponentRef={inst => (this.formEditor = inst)}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.carrier.create,
        organizations: state.home.organizations
    };
};

export default connect(mapStateToProps)(CarrierModal);
